package com.liu.frame.http;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.liu.frame.application.MyApplication;
import com.liu.frame.beans.UserBean;
import com.liu.frame.cache.CacheConfig;
import com.liu.frame.config.Api;
import com.liu.frame.config.Constant;
import com.liu.frame.utils.Base64Utils;
import com.liu.frame.utils.LogUtils;
import com.liu.frame.utils.RSAUtils;
import com.liu.frame.utils.SharedPreferencesUtils;
import com.liu.frame.utils.TokenUtils;

import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

import cn.finalteam.okhttpfinal.RequestParams;

/**
 * Created by 刘智强 on 2016/10/31.
 * 对网络请求的参数作处理
 */

public class WeiLiCaiHttpParameters implements IWeiLiCaiHttpParameters {

    public HashMap<String, String> allParameters;//所有的参数
    private HashMap<String, String> publicParameters;//公共参数
    private HashMap<String, String> privateParameters;//每个接口的私有参数
    private PublicKey publicKey;//加密的密匙


    public WeiLiCaiHttpParameters() {
        this.allParameters = new HashMap<>();
        this.publicParameters = new HashMap<>();
        this.privateParameters = new HashMap<>();
        this.publicKey = RSAUtils.loadPublicKey();//rsa 密钥

    }

    @Override
    public RequestParams postParameters() {
        return null;
    }

    @Override
    public void postPublicParameters() {
        publicParameters.put("appType", Constant.appType);
        publicParameters.put("appVersion", MyApplication.getInstance().appVersion);
        publicParameters.put("qType", Constant.qTypeApi);
        publicParameters.put("serviceVersion", Constant.serviceVersion);

    }

    @Override
    public void postPrivateParameters(String key, String value) {

        privateParameters.put(key, value);
    }

    @Override
    public HashMap<String, String> postAllParameters(HashMap<String, String> privateParameters) {

        UserBean userBean;
        String appLoginKey = "";
        String userId = "0";

        userBean = new SharedPreferencesUtils(MyApplication.getInstance().getApplicationContext()).getObject(Api.LOGIN, UserBean.class);
        if (userBean != null) {
            appLoginKey = userBean.getApp_login_key();
            userId = userBean.getUser_id();
        }

        String[] parameters = new String[]{Constant.qTypeApi, "" + System.currentTimeMillis() / 1000, appLoginKey, userId};
        postPublicParameters();
        if (!privateParameters.isEmpty()) {
            for (Map.Entry<String, String> entry : privateParameters.entrySet()) {
                LogUtils.e("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                if (!entry.getKey().isEmpty() && !entry.getValue().isEmpty()) {
                    /**
                     * 对私有参数加密
                     */
                    postPrivateParameters(entry.getKey(), entry.getValue());
                    String value = entry.getValue();
                    byte[] encryptByte = RSAUtils.encryptData(value.getBytes(), publicKey);
                    value = Base64Utils.encode(encryptByte);
                    allParameters.put(entry.getKey(), value);
                }
            }

        }
        if (MyApplication.getInstance().isLogin()) {
            /**
             * 传入需要登录的token
             */
            allParameters.put("token", TokenUtils.getLoginNeedToken(privateParameters, parameters));

        } else {
            /**
             * 非登陆状态下传入的TOKEN
             */
            allParameters.put("token", TokenUtils.getLoginNotNeedToken(privateParameters, parameters[0]));
        }
        allParameters.putAll(publicParameters);
        return allParameters;
    }


}
